import datetime
from static.connect.conn_mysql import Connect


class Card(Connect):

    def access_tokens(self, access_token, user_id):
        if not all([access_token, user_id]):
            return {
                'status': -1,
                'errorCode': 11,
                'errorMsg': 'access_token为空',
                'resultBody': None
            }
        select_sql = 'select * from access_token where access_token=%s and user_id=%s'
        result = self.select_one(select_sql, (access_token, user_id))

        if not result:
            return {
                'status': -1,
                'errorCode': 11,
                'errorMsg': 'access_token在数据库中没有匹配的值',
                'resultBody': None
            }
        else:
            token_time = result.get('regedate')
            if token_time + datetime.timedelta(hours=2) <= datetime.datetime.now():
                return {
                    'status': -1,
                    'errorCode': 12,
                    'errorMsg': 'access_token已经过期,请重新登录',
                    'resultBody': None
                }
        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': '验证通过,可以继续操作'
        }
